home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / LIB / MUI / GLUTMUI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  5.4 KB  |  185 lines

  1. /*
  2.  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. #include <stdlib.h>
  39. #include <GL/glut.h>
  40. #include <mui/gizmo.h>
  41.  
  42. typedef struct _Window {
  43.   int mui_xorg, mui_yorg, mui_xsize, mui_ysize;
  44.   int uilist;
  45.   int mbleft;
  46. } WindowRec, *Window;
  47.  
  48. static Window winList = NULL;
  49. static int numWins = 0;
  50.  
  51. int mui_singlebuffered = 0;
  52.  
  53. int menuinuse = 0;
  54.  
  55. static int mbmiddle = 0, mbright = 0;  /* XXX unused currently */
  56.  
  57. void setmousebuttons(int b, int s)
  58. {
  59.     Window win = &winList[glutGetWindow()-1];
  60.  
  61.     switch (b) {
  62.     case GLUT_LEFT_BUTTON:
  63.         win->mbleft = (s == GLUT_DOWN);
  64.         break;
  65.     case GLUT_MIDDLE_BUTTON:
  66.         mbmiddle =(s == GLUT_DOWN);
  67.         break;
  68.     case GLUT_RIGHT_BUTTON:
  69.         mbright =(s == GLUT_DOWN);
  70.         break;
  71.     }
  72. }
  73.  
  74. void mui_drawgeom(void)
  75. {
  76.     Window win = &winList[glutGetWindow()-1];
  77.  
  78.     glViewport(0, 0, win->mui_xsize, win->mui_ysize);
  79.     glMatrixMode(GL_PROJECTION);
  80.     glLoadIdentity();
  81.     gluOrtho2D(0, win->mui_xsize, 0, win->mui_ysize);
  82.     glMatrixMode(GL_MODELVIEW);
  83.     glLoadIdentity();
  84.     muiDrawUIList(win->uilist);
  85.     if (mui_singlebuffered == 0)
  86.     glutSwapBuffers();
  87.     else
  88.     glFlush();
  89. }
  90.  
  91. void mui_keyboard(unsigned char c, int x, int y)
  92. {
  93.     Window win = &winList[glutGetWindow()-1];
  94.  
  95.     muiSetActiveUIList(win->uilist);
  96.     muiHandleEvent(MUI_KEYSTROKE, c, x, win->mui_ysize-y);
  97.     glutPostRedisplay();
  98. }
  99.  
  100. void mui_mouse(int b, int s, int x, int y)
  101. {
  102.     Window win = &winList[glutGetWindow()-1];
  103.  
  104.     muiSetActiveUIList(win->uilist);
  105.     setmousebuttons(b, s);
  106.     if (b == GLUT_MIDDLE_BUTTON && s == GLUT_DOWN) {
  107.     muiHandleEvent(MUI_DEVICE_DOUBLE_CLICK, 0, x, win->mui_ysize-y);
  108.     glutPostRedisplay();
  109.     }
  110.     if (b != GLUT_LEFT_BUTTON) { return; }
  111.     muiHandleEvent((s==GLUT_DOWN)?MUI_DEVICE_PRESS:MUI_DEVICE_RELEASE, 0, x, win->mui_ysize-y);
  112.     glutPostRedisplay();
  113. }
  114.  
  115. static void mui_Reshape(int width, int height)
  116. {
  117.     Window win = &winList[glutGetWindow()-1];
  118.  
  119.     win->mui_xorg = glutGet(GLUT_WINDOW_X);
  120.     win->mui_yorg = glutGet(GLUT_WINDOW_Y);
  121.     win->mui_xsize = width;
  122.     win->mui_ysize = height;
  123.     glViewport(0, 0, win->mui_xsize, win->mui_ysize);
  124. }
  125.  
  126. void mui_glutmotion(int x, int y)
  127. {
  128.     Window win = &winList[glutGetWindow()-1];
  129.  
  130.     muiSetActiveUIList(win->uilist);
  131.     if (win->mbleft == 0) return;
  132.     muiHandleEvent(MUI_DEVICE_DOWN, 0, x, win->mui_ysize-y);
  133.     glutPostRedisplay();
  134. }
  135.  
  136. void mui_glutpassivemotion(int x, int y)
  137. {
  138.     Window win = &winList[glutGetWindow()-1];
  139.  
  140.     muiSetActiveUIList(win->uilist);
  141.     muiHandleEvent(MUI_DEVICE_UP, 0, x, win->mui_ysize-y);
  142.     glutPostRedisplay();
  143. }
  144.  
  145. void mui_menufunc(int state)
  146. {
  147.     menuinuse = state;
  148. }
  149.  
  150. void muiInit(void)
  151. {
  152.     int winNum = glutGetWindow();
  153.     Window win;
  154.  
  155.     if (winNum >= numWins) {
  156.       numWins = winNum;
  157.       winList = (Window) realloc(winList, numWins * sizeof(WindowRec));
  158.     }
  159.     win = &winList[glutGetWindow()-1];
  160.     win->mui_xorg = glutGet(GLUT_WINDOW_X);
  161.     win->mui_yorg = glutGet(GLUT_WINDOW_Y);
  162.     win->mui_xsize = glutGet(GLUT_WINDOW_WIDTH);
  163.     win->mui_ysize = glutGet(GLUT_WINDOW_HEIGHT);;
  164.     win->mbleft = 0;
  165.     /* The "uilist = 1" is for compatibility with GLUT 3.5's MUI
  166.        implementation that was hardwired to support a single window
  167.        only with UI list 1. */
  168.     win->uilist = 1;
  169.  
  170.     glutKeyboardFunc(mui_keyboard);
  171.     glutMouseFunc(mui_mouse);
  172.     glutReshapeFunc(mui_Reshape);
  173.     glutMotionFunc(mui_glutmotion);
  174.     glutPassiveMotionFunc(mui_glutpassivemotion);
  175.     glutDisplayFunc(mui_drawgeom);
  176.     glutMenuStateFunc(mui_menufunc);
  177. }
  178.  
  179. void muiAttachUIList(int uilist)
  180. {
  181.   Window win = &winList[glutGetWindow()-1];
  182.  
  183.   win->uilist = uilist;
  184. }
  185.